package com.sxhuayuan.parking.compenent.shiro;

import java.util.Collection;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.realm.Realm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyModularRealmAuthenticator extends ModularRealmAuthenticator {

	private static final Logger log = LoggerFactory.getLogger(MyModularRealmAuthenticator.class);

	@Override
	protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
		assertRealmsConfigured();
		Collection<Realm> realms = getRealms();
		com.sxhuayuan.parking.compenent.shiro.AuthenticationToken token = (com.sxhuayuan.parking.compenent.shiro.AuthenticationToken) authenticationToken;
		log.debug("authenticationToken=" + authenticationToken);
		for (Realm r : realms) {
			if (r.getName().equalsIgnoreCase(token.getAccountType().name())) {
				log.debug("realm.name={}", r.getName());
				return doSingleRealmAuthentication(r, authenticationToken);
			}
		}
		return super.doAuthenticate(authenticationToken);
	}
}
